package com.example.myapplication.adapter;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.PlayService;
import com.example.myapplication.R;
import com.example.myapplication.acticity.Mp3Activity;
import com.example.myapplication.utils.MediaUtils;
import com.example.myapplication.vo.Mp3Info;

import java.util.List;

public class MyMusicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private final int N_TYPE = 0;
    private final int F_TYPE = 1;
    private  int MAX_NUM = 12;//最大显示数量
    private Boolean isFootView = true;//是否为底部加载view
    private List<Mp3Info> data;
    private OnItemClickListener onItemClickListener;//点击

    public MyMusicAdapter(Context context , List<Mp3Info> data){
        this.context = context;
        this.data = data;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    //点击回调接口
    public interface OnItemClickListener{
        public void OnItemClick(View view,int position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemo,parent,false);
        View footview = LayoutInflater.from(parent.getContext()).inflate(R.layout.foot_item,parent,false);

        if (viewType == F_TYPE){
            return new RecyclerViewHolder(footview,F_TYPE);
        }else {
            return new RecyclerViewHolder(view,N_TYPE);

        }

    }



    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (isFootView && (getItemViewType(position)) == F_TYPE){
            final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
            recyclerViewHolder.loding.setText("加载中........");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    MAX_NUM += 8;
                    notifyDataSetChanged();
                }
            },1000);

        }else {
            final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
            Mp3Info mp3Info = data.get(position);
            recyclerViewHolder.artist.setText(mp3Info.getArtist());
            recyclerViewHolder.title.setText(mp3Info.getTitle());
            recyclerViewHolder.duration.setText(MediaUtils.formatTime(mp3Info.getDuration()));

            /**
             * item设置点击事件，用外部回调的数据来设置点击
             */
            recyclerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = recyclerViewHolder.getAdapterPosition();
                    onItemClickListener.OnItemClick(view,position);
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        if (data.size() < MAX_NUM){
            return data.size();
        }else {
            return MAX_NUM;
        }
    }
    /**
     * 如果当前位置是显示最大值-1，那么就代表当前就是最后一个可显示的位置
     * 就代表当前应该显示foot
     * 如果不是就正常显示
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (position == MAX_NUM-1){
            return F_TYPE;
        }else {
            return N_TYPE;
        }
    }
    private class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView artist,title,duration,loding;

        public RecyclerViewHolder(View Itemview, int viewType) {
            super(Itemview);
            if (viewType == N_TYPE) {
                artist = Itemview.findViewById(R.id.artist);
                title = Itemview.findViewById(R.id.titlem);
                duration = Itemview.findViewById(R.id.duration);
            }else if (viewType == F_TYPE) {
                loding = Itemview.findViewById(R.id.loding);
            }
        }
    }
}
